package com.whfc.ms.api.sys.service.impl;

import com.whfc.common.enums.SysUserType;
import com.whfc.common.result.PageData;
import com.whfc.fuum.dto.SysDeptRoleDTO;
import com.whfc.fuum.dto.SysRoleDTO;
import com.whfc.fuum.dto.SysRuleDTO;
import com.whfc.fuum.dto.SysUserDTO;
import com.whfc.fuum.entity.SysEmpUser;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.*;
import com.whfc.fuum.service.SysUserService;
import com.whfc.ms.api.sys.service.MsSysUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2020-12-11 15:06
 */
@Service
public class MsSysUserServiceImpl implements MsSysUserService {


    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;


    @Override
    public PageData<SysUserDTO> list(SysUser loginUser, SysUserListParam sysUserParam) {
        return sysUserService.list(loginUser, sysUserParam);
    }

    @Override
    public SysUserDTO queryUserById(Integer userId) {
        return sysUserService.queryUserById(userId);
    }

    @Override
    public void addDeptUser(SysDeptUserAdd deptUserAdd) {
        sysUserService.addDeptUser(deptUserAdd);
    }

    @Override
    public void addEmpUser(SysEmpUserAdd empUserAdd){
        sysUserService.addEmpUser(empUserAdd);
    }

    @Override
    public SysEmpUser getEmpUser(Integer empId){
        return sysUserService.getEmpUser(empId);
    }

    @Override
    public void addDeptRoles(SysUserDeptRolesAdd userDeptRolesAdd) {
        sysUserService.addDeptRoles(userDeptRolesAdd);
    }

    @Override
    public void editDeptUser(SysDeptUserEdit deptUserEdit) {
        sysUserService.editDeptUser(deptUserEdit);
    }

    @Override
    public void del(SysUser sysUser, Integer userId) {
        sysUserService.del(sysUser, userId);
    }

    @Override
    public void edit(SysUserEdit sysUserEdit) {
        sysUserService.edit(sysUserEdit);
    }

    @Override
    public void enable(Integer id, Integer status, Integer userId) {
        sysUserService.enable(id, status, userId);
    }

    @Override
    public void updatePassword(SysUser sysUser, UpdatePasswordParam request) {
        sysUserService.updatePassword(sysUser, request);
    }

    @Override
    public void resetPassword(SysResetPasswordParam request) {
        sysUserService.resetPassword(request);
    }

    @Override
    public List<SysRuleDTO> getUserRuleList(SysUser loginUser, Integer platform) {
        return sysUserService.getUserRuleList(loginUser, platform);
    }

    @Override
    public List<SysRuleDTO> getUserRuleListByDeptId(SysUser loginUser, Integer platform, Integer deptId) {
        return sysUserService.getUserRuleListByDeptId(loginUser, platform, deptId);
    }

    @Override
    public PageData<SysUserDTO> innerUserList(Integer pageNum, Integer pageSize, String keyword) {
        return sysUserService.innerUserList(pageNum, pageSize, keyword);
    }

    @Override
    public void innerUserAdd(SysInnerUserAddParam request) {
        sysUserService.innerUserAdd(request);
    }

    @Override
    public void innerUserEdit(SysInnerUserEditParam request) {
        sysUserService.innerUserEdit(request);
    }

    @Override
    public List<SysDeptRoleDTO> getUserDeptRoles(SysUser sysUser, Integer userId, Integer deptId) {
        //获取需要展示的用户组织机构权限
        List<SysDeptRoleDTO> userDeptRoles = sysUserService.getUserDeptRoles(userId, deptId);
        //内部用户直接返回
        if (SysUserType.INNER.getValue().equals(sysUser.getType())) {
            return userDeptRoles;
        }
        //获取当前用户的组织机构权限
        List<SysDeptRoleDTO> currUserDeptRoles = sysUserService.getUserDeptRoles(sysUser.getId(), deptId);
        //拿到当前用户的组织机构已经角色id
        List<Integer> deptIdList = new ArrayList<>();
        List<Integer> roleIdList = new ArrayList<>();
        for (SysDeptRoleDTO currUserDeptRole : currUserDeptRoles) {
            deptIdList.add(currUserDeptRole.getDeptId());
            List<SysRoleDTO> roleList = currUserDeptRole.getRoleList();
            for (SysRoleDTO sysRoleDTO : roleList) {
                roleIdList.add(sysRoleDTO.getId());
            }
        }
        //返回的用户组织机构权限
        List<SysDeptRoleDTO> resultUserDeptRoles = new ArrayList<>();

        //对需要展示的用户组织机构权限做处理
        for (SysDeptRoleDTO userDeptRole : userDeptRoles) {
            //当前用户能看到的组织机构才展示
            if (!deptIdList.contains(userDeptRole.getDeptId())) {
                continue;
            }
            //用户角色列表
            List<SysRoleDTO> list = new ArrayList<>();
            List<SysRoleDTO> roleList = userDeptRole.getRoleList();
            for (SysRoleDTO sysRoleDTO : roleList) {
                if (roleIdList.contains(sysRoleDTO.getId())) {
                    list.add(sysRoleDTO);
                }
            }
            userDeptRole.setRoleList(list);
            resultUserDeptRoles.add(userDeptRole);
        }
        return resultUserDeptRoles;
    }

    @Override
    public void editDeptRoles(SysUserRoleEdit request) {
        sysUserService.editDeptRoles(request);
    }
}
